//通用微信分享处理（只在初次载入或刷新页面时执行，pageinit不会执行）
define(function(){
//成功后回调
var _shareCallOK = function(){
 
}
 

function _wxconfig(wx,appId,timestamp,nonceStr,signature){
 wx.config({
      debug: false,
      appId: appId,
      timestamp: timestamp,
      nonceStr: nonceStr,
      signature: signature,
      jsApiList: [
        'checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
		'hideOptionMenu',
		'showOptionMenu',
		'hideMenuItems',
		'showMenuItems',
		'hideAllNonBaseMenuItem',
		'showAllNonBaseMenuItem' 
      
      ]
  });
}



function _menuShare(wx){
    if(!isWeixin()){return;}
    var linkurl = getShareLinkUrl(window.location.href);//分享URL
	var title = "最专业的考试题库助你考试顺利通过";
	var desc = "66考试题库，手机做题轻松过关！";
    var imgUrl= "http://vip.niukewang.com/wxfx300.png";

	getJSSDKSign(
		window.location.href.split('#')[0],//签名URL
		function(data){
	         _wxconfig(
			 wx,
			 data.appid,
			 data.timestamp,
			 data.nonceStr,
			 data.signature);
            _wxready(wx,linkurl,title,desc,imgUrl);
			/*
            //获取推荐人aid参数
			isCheckUserLogin(function(data){

			 var aid = data.accountId;
			 var par = "?";
			 if(linkurl.indexOf("?")>-1){
			  par = "&";
			 }
			 if(linkurl.indexOf("aid=")>-1){
				 linkurl=_replaceParamVal(linkurl,"aid",aid);
			 }else{
				 linkurl += par+"aid="+aid;
			 }

			  _wxready(wx,linkurl,title,desc,imgUrl);
			},
			function(){
			  _wxready(wx,linkurl,title,desc,imgUrl);
			});
		 */
		},
		function(data){
		});
		 
}

var _wxready=function(wx,linkurl,title,desc,imgUrl){
			wx.ready(function () { 
			//wx.hideAllNonBaseMenuItem();
			//wx.showMenuItems({ menuList: ["menuItem:share:appMessage","menuItem:share:timeline","menuItem:share:qq","menuItem:share:QZone"]  });
			//分享到朋友圈
			wx.onMenuShareTimeline({
			 
			title: title,
			desc: desc, // 分享描述
			link: linkurl,  
			imgUrl: imgUrl,  
			success: function () { 
				_shareCallOK();//确认分享 
			},
			cancel: function () { 
				 
			}
			});

			wx.onMenuShareAppMessage({
			title: title,  
			desc: desc,  
			link: linkurl,   
			imgUrl: imgUrl,  
			type: '', // 分享类型,music、video或link，不填默认为link
			dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
			success: function () { 
			  _shareCallOK(); 
			},
			cancel: function () { 
			 
			}
			});


			wx.onMenuShareWeibo({
			title: title,  
			desc: desc,  
			link: linkurl,    
			imgUrl: imgUrl,   
			success: function () { 
			 _shareCallOK(); 
			},
			cancel: function () { 
			 
			}
			});

			wx.onMenuShareQQ({
			title: title,  
			desc: desc,  
			link: linkurl,   
			imgUrl: imgUrl,   
			success: function () { 
			_shareCallOK(); 
			},
			cancel: function () { 
			 
			}
			});


			wx.onMenuShareQZone({
			title: title,  
			desc: desc,  
			link: linkurl,  
			imgUrl: imgUrl,   
			success: function () { 
			_shareCallOK(); 
			},
			cancel: function () { 
			 
			}
			});
			});
			wx.error(function (res) {alert(res.errMsg);});
  
    }
  return {menuShare:_menuShare}
});